package com.bestcem.xm.txtanls.controller.param.chart;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.lang.Validator;
import com.bestcem.xm.common.core.constant.Constants;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.txtanls.enums.IntervalEnum;
import com.bestcem.xm.txtanls.grpc.service.param.chart.BaseChartParam;
import com.bestcem.xm.txtanls.service.dto.chart.PeriodTimeDTO;
import com.bestcem.xm.txtanls.service.dto.chart.TimeRangeDTO;
import com.bestcem.xm.txtanls.util.ChartUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 文本分析请求基础参数
 * @author jy.zhao
 * @version 2.0
 * @date 2021/8/5 17:42
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(description = "通用文本分析查询条件")
public class BaseChartQuery {

    @ApiModelProperty(value = "文本分析项目id")
    @JsonProperty(value = "project_id")
    protected String projectId;

    @ApiModelProperty(value = "分析的字段id")
    @JsonProperty(value = "field_id")
    protected String fieldId;

    @ApiModelProperty(value = "时间间隔类型, day week month quarter year custom", example = "day")
    @JsonProperty(value = "dtfiltertype")
    protected String filterType;

    @ApiModelProperty(value = "根据时间间隔的开始时间", example = "\"2020-06-03\"")
    protected String current;

    @ApiModelProperty(value = "自定义开始时间，yyyy-MM-dd", example = "\"2020-06-03\"")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @JsonProperty(value = "start_time")
    protected LocalDate startTime;

    @ApiModelProperty(value = "自定义结束时间，yyyy-MM-dd", example = "\"2020-06-03\"")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @JsonProperty(value = "end_time")
    protected LocalDate endTime;

    /**
     * filterType 转对应的枚举类.默认是天
     * @return Interval 时间间隔枚举
     * @author jy.zhao
     * @date 2021/8/5 19:58
     **/
    public IntervalEnum getInterval() {
        IntervalEnum interval;
        if(IntervalEnum.DAY.getName().equals(filterType)) {
            interval = IntervalEnum.DAY;
        } else if(IntervalEnum.WEEK.getName().equals(filterType)) {
            interval = IntervalEnum.WEEK;
        } else if(IntervalEnum.MONTH.getName().equals(filterType)) {
            interval = IntervalEnum.MONTH;
        } else if(IntervalEnum.QUARTER.getName().equals(filterType)) {
            interval = IntervalEnum.QUARTER;
        } else if(IntervalEnum.YEAR.getName().equals(filterType)) {
            interval = IntervalEnum.YEAR;
        } else if(IntervalEnum.CUSTOM.getName().equals(filterType)) {
            interval = IntervalEnum.CUSTOM;
        } else {
            interval = IntervalEnum.DAY;
        }
        return interval;
    }

    /**
     * 获取查询参数
     * @param
     * @return BaseChartParam 查询参数
     * @author jy.zhao
     * @date 2021/8/5 20:34
     **/
    @JsonIgnore
    public BaseChartParam getBaseChartParam() {
        // 如果current为null，赋值昨天的时间
        if(Validator.isEmpty(current)) {
            current = LocalDateTimeUtil.format(DateUtil.getCurrentTime(), DateTimeFormatter.ofPattern(Constants.DATE_FORMAT));
        }
        // 如果时间间隔为季度，传递的startTime格式为2021Q1
        if(IntervalEnum.QUARTER.getName().equals(filterType)) {
            String quarter = current.substring(4, 6);
            current = current.substring(0, 4) + Constants.HYPHEN + Constants.QUARTER_START_TIME_MAP.get(quarter);
        }
        PeriodTimeDTO periodTimeDTO;
        // filterType 转对应的枚举类数值
        Integer interval = getInterval().getIndex();
        if(IntervalEnum.CUSTOM.getName().equals(filterType)) {
            periodTimeDTO = new PeriodTimeDTO();
            if(Validator.isNotNull(startTime)) {
                periodTimeDTO.setStartTime(startTime.atStartOfDay());
            }
            if(Validator.isNotNull(endTime)) {
                periodTimeDTO.setEndTime(endTime.atTime(23, 59, 59));
            }
        } else {
            periodTimeDTO = ChartUtil.getCurPeriodTime(current, interval);
        }
        // 封装timeRangeDTO 时间范围dto
        TimeRangeDTO timeRangeDTO = new TimeRangeDTO();
        timeRangeDTO.setInterval(interval);
        timeRangeDTO.setPeriodTime(periodTimeDTO);

        // 封装查询参数baseChartParam.
        BaseChartParam baseChartParam = new BaseChartParam();
        baseChartParam.setProjectId(this.projectId);
        baseChartParam.setFieldId(this.fieldId);
        baseChartParam.setTimeRangeDTO(timeRangeDTO);
        return baseChartParam;
    }
}
